package com.app.newiptv.utils;

import android.animation.ValueAnimator;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.animation.Animation;

public class FocusedViewUtils {
    public static final float ZOOM_IN = 1.1f;
    public static final float ZOOM_OUT = 1.0f;
    public static final int DURATION = 200;

    /**
     * 移动焦点框至指定的VIEW上
     *
     * @param focusedFrame 焦点框
     * @param view         焦点VIEW
     * @param offsetLeft   焦点框左边距
     * @param offsetTop    焦点框上边距
     * @param offsetRight  焦点框右边距
     * @param offsetBottom 焦点框下边距
/     */
    public static void setFocusedView(View focusedFrame, View view, int offsetLeft, int offsetTop, int offsetRight, int offsetBottom) {
        setFocusedView(focusedFrame, view, offsetLeft, offsetTop, offsetRight, offsetBottom, true);
    }

    private static void setFocusedView(View focusedFrame, View view, int offsetLeft, int offsetTop, int offsetRight, int offsetBottom, boolean zoomIn) {
    	if(focusedFrame != null){
    		focusedFrame.setVisibility(View.VISIBLE);
    	}
    	if (zoomIn) {
            ValueAnimator viewZoomInAnim = ValueAnimator.ofFloat(ZOOM_OUT, ZOOM_IN);
            viewZoomInAnim.addUpdateListener(new ViewAnimListener(focusedFrame, view, offsetLeft, offsetTop, offsetRight, offsetBottom));
            viewZoomInAnim.setDuration(DURATION);
            viewZoomInAnim.start();
        }
    }

    public static void setBlurView(View focusedFrame, View view) {
    	if(focusedFrame != null){
    		focusedFrame.setVisibility(View.GONE);
    	}
        ValueAnimator viewZoomInAnim = ValueAnimator.ofFloat(ZOOM_IN, ZOOM_OUT);
        viewZoomInAnim.addUpdateListener(new ViewAnimListener(view));
        viewZoomInAnim.setDuration(DURATION);
        viewZoomInAnim.start();
    }
    public static boolean arrowScroll(View view) {
        int right = view.getRight();
        int left = view.getLeft();
        int top = view.getTop();
        int bottom = view.getBottom();

        ViewGroup parentView = (ViewGroup) view.getParent();
        int width = parentView.getWidth();
        int height = parentView.getHeight();
        int paddingLeft = parentView.getPaddingLeft();
        int paddingRight = parentView.getPaddingRight();
        int paddingTop = parentView.getPaddingTop();
        int paddingBottom = parentView.getPaddingBottom();
        return left < paddingLeft || right > width - paddingRight || top < paddingTop || bottom > height - paddingBottom;
    }
    static class ViewAnimListener implements ValueAnimator.AnimatorUpdateListener, Animation.AnimationListener {
    	private int LEFT = 0;
        private int TOP = 1;
    	private View view;
        private View shadeFrame;
        private int offsetLeft;
        private int offsetTop;
        private int offsetRight;
        private int offsetBottom;
        public ViewAnimListener(View view) {
			this.view = view;
		}

		public ViewAnimListener(View shadeFrame, View view, int offsetLeft, int offsetTop, int offsetRight, int offsetBottom) {
            this.shadeFrame = shadeFrame;
            this.view = view;
            this.offsetLeft = offsetLeft;
            this.offsetTop = offsetTop;
            this.offsetRight = offsetRight;
            this.offsetBottom = offsetBottom;
            init();
        }

        private void init() {
            int[] viewLeftAndTop = new int[2];
            view.getLocationOnScreen(viewLeftAndTop);       
            int itemWidth = view.getWidth();
            int itemHeight = view.getHeight();
            
            MarginLayoutParams params = (MarginLayoutParams) shadeFrame.getLayoutParams();        
            params.leftMargin = viewLeftAndTop[LEFT] - offsetLeft;
            params.topMargin = viewLeftAndTop[TOP] - offsetTop;
            params.width = itemWidth + offsetLeft + offsetRight;
            params.height = itemHeight + offsetTop + offsetBottom;
            shadeFrame.setLayoutParams(params);
        }       
    
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            float value = (float) animation.getAnimatedValue();
            if(shadeFrame != null) {
            	shadeFrame.setScaleX(value);
            	shadeFrame.setScaleY(value);
            }
            view.setScaleX(value);
            view.setScaleY(value);
        }

        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {

        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    }
}
